<?php
/*
 +----------------------------------------------------------------------
 + Title        : Banner 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2021/3/8 10:30
 + Last-time    : 2021/3/8 10:30+ chen2
 + Desc         : Banner
 +----------------------------------------------------------------------
*/


namespace app\admin\controller;


use app\common\service\BannerService;
use app\common\validate\BannerValidate;
use think\App;

use function get_client_id;

class BannerController extends AdminBaseController {

	protected $validate;
	protected $service;

	public function __construct(App $app) {
		parent::__construct($app);
		$this->validate = new BannerValidate();
		$this->service = new BannerService();
	}

	public function index() {
		$data = $this->getParams();
		$this->validate->extractScene($data, 'index')
		               ->failException(true)
		               ->check($data);
		$data = $this->request->param();
		$res = $this->service->getBannerList($data);
		return $this->returnSuccess($res);
	}

	public function read( $id ){
		$res = $this->service->getBannerItem($id);
		return $this->returnSuccess($res);
	}

	/**
	 *
	 * @throws \Exception
	 * @author : Randy_chen
	 * @Date   : 2021/2/27
	 * @Time   : 22:34
	 */
	public function save() {
		$data = $this->getParams();
		$this->validate->extractScene($data, 'save')
		               ->failException(true)
		               ->check($data);
		$result =  $this->service->addBannerItem($data, get_client_id());
		return $this->returnSuccess($result);
	}

	public function update($id) {
		$data = $this->request->param();
		$data['uuid'] = $id;
		$this->validate->extractScene($data, 'update')
		               ->failException(true)
		               ->check($data);
		$res =  $this->service->setBannerItem($data);
		return $this->returnSuccess($res);
	}

	//删除
	public function delete($id) {
        $data['uuid'] = $id;
        $this->validate->extractScene($data, 'delete')
                       ->failException(true)
                       ->check($data);
        $res = $this->service->delBannerItem($id);
        return $this->returnSuccess($res);
    }
}